package es.supermanager;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;


public class ExtraerCodigos extends ParserCallback {

	String path;
	String [] direcciones;
	int contador;
	
	public void setPath (String path) {
		this.path = path;
	}
	
	public String [] obtenerCodigos () {

		contador = 0;
		
		direcciones = new String[200];
		
		try {
			
			BufferedReader bf = new BufferedReader(new FileReader(path));

			String s, texto="";
			while ((s = bf.readLine())!= null) {
				texto = texto + s + "\n";
			}
			
//			System.out.println(texto);
			
			StringReader r = new StringReader(texto);
			HTMLEditorKit.Parser parse = new HTMLParse().getParser();

			parse.parse(r,this,true);

		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String [] codigos = sacarCodigos();
		
		return codigos;

	}
	
	public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {

//		System.out.println("Tag: " + t.toString());
		
		Object value = a.getAttribute(HTML.Attribute.HREF);
		
		if (value!=null) {
//			System.out.println("HREF: " + value.toString());
			if (value.toString().startsWith("http://www.acb.com/stspartidojug.php")) {
				direcciones[contador] = value.toString();
				contador++;
			}
		}
		

	}
	
		
	public String [] sacarCodigos () {
		String [] codigos = new String[200];
		
		for (int i=0; i<direcciones.length; i++){
			if (direcciones[i]==null) break;
			codigos[i] = direcciones[i].substring(49);
		}
//		System.out.println(direcciones[0].substring(49));
		return codigos;
	}
	
	
	
}

	